package com.haoshuang.zhouzhoubang.ui.page.home.settings.skill.add

import android.content.Context
import android.os.Bundle
import android.util.Log
import com.gyf.immersionbar.ktx.immersionBar
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityAddSkillListBinding
import com.haoshuang.zhouzhoubang.model.bean.SkillAddBean
import com.haoshuang.zhouzhoubang.ui.dialog.ConfirmDialog
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.toJson
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.invisible
import com.mufeng.libs.utils.view.removeAllAnimation
import com.mufeng.libs.utils.view.textColor
import com.mufeng.libs.utils.view.vertical
import com.mufeng.libs.utils.view.visible

/**
 *  门店技能添加服务类别
 */
class AddSkillListActivity : BaseVMActivity<AddSkillListViewModel, ActivityAddSkillListBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<AddSkillListActivity>()
        }
    }

    private var type = 1

    private val leftData = arrayListOf<SkillAddBean>()
    private val rightData = arrayListOf<SkillAddBean.Small>()

    private val leftAdapter = AddSkillLeftAdapter(leftData)
    private val rightAdapter = AddSkillRightAdapter(rightData)

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "服务类别"
        binding.titleBar.leftImageView().click { finish() }

        binding.llYx.clickWithTrigger {
            if (type != 1){
                type = 1
                rightAdapter.type = 1
                binding.tvYx.textColor = R.color.c_3b76c2
                binding.vLine.visible()
                binding.tvYx2.textColor = R.color.c_66
                binding.vLine2.invisible()
                vm.getAddSkillList(type)
                binding.llNext.visible()
                binding.tvQrz.text = "添加"
            }
        }
        binding.llYx2.clickWithTrigger {
            if (type != 2){
                type = 2
                rightAdapter.type = 2
                binding.tvYx.textColor = R.color.c_66
                binding.vLine.invisible()
                binding.tvYx2.textColor = R.color.c_3b76c2
                binding.vLine2.visible()
                vm.getAddSkillList(type)
                binding.llNext.visible()
                binding.tvQrz.text = "移除"
            }
        }

        binding.rvList.vertical().adapter = leftAdapter
        binding.rvList.removeAllAnimation()
        binding.rvList2.vertical().adapter = rightAdapter
        binding.rvList2.removeAllAnimation()

        leftAdapter.setOnItemClick { view, position, item ->
            leftData.forEach {
                it.is_checked = item?.id == it.id
            }
            leftAdapter.notifyDataSetChanged()
            rightData.clear()
            rightData.addAll(item?.small ?: listOf())
            rightAdapter.notifyDataSetChanged()
        }

        binding.tvQrz.clickWithTrigger {
            val firstList = arrayListOf<SkillAddBean>()
            val secondList = arrayListOf<SkillAddBean.Small>()
            val thirdList = arrayListOf<SkillAddBean.Small.LittleCate>()
            val list = leftData.filter {
                val list = it.small?.filter {
                    val list = it.little_cate?.filter { it.is_checked == true }
                    thirdList.addAll(list?: listOf())
                    list?.isNotEmpty() ?: false
                }
                secondList.addAll(list?: listOf())
                list?.isNotEmpty() ?: false
            }
            firstList.addAll(list)
            if (thirdList.isEmpty()){
                toast("请选择技能")
                return@clickWithTrigger
            }
            var id1s = firstList.joinToString(","){it.id.toString()}
            var id2s = secondList.joinToString(","){it.id.toString()}
            val id3s = thirdList.joinToString(","){it.id.toString()}
            if (type == 1){
                // 添加技能
                vm.updateTeamCategory(id1s, id2s, id3s)
            }else{
                val list1 = arrayListOf<SkillAddBean>()
                val list2 = arrayListOf<SkillAddBean.Small>()
                leftData.forEach {
                    if (it.small?.filter { it.is_checked != true }.isNullOrEmpty()){
                        // 全部全选  添加一级
                        list1.add(it)
                    }
                    it.small?.forEach {
                        if (it.is_checked == true){
                            // 全部全选  添加二级
                            list2.add(it)
                        }
                    }
                }
                id1s = list1.joinToString(","){it.id.toString()}
                id2s = list2.joinToString(","){it.id.toString()}
                XPopup.Builder(mActivity)
                    .asCustom(ConfirmDialog(
                        context =mActivity,
                        title = "温馨提示",
                        content = "是否移除所选技能?",
                        cancelStr = "取消",
                        confirmStr = "确认移除",
                        isHideCancel = false,
                        isHideAgain = false,
                        onCancel = {},
                        onConfirm = {
                            // 取出二级全选的列表
                            vm.undockTeamCategory(id1s, id2s, id3s)
                        }))
                    .show()

            }
        }

    }

    override fun initData() {
        vm.getAddSkillList(type)
    }

    override fun startObserve() {

        vm.updateTeamCategoryData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                toast("添加成功, 等待审核")
                setResult(200)
                finish()
            }
        }

        vm.undockTeamCategoryData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                toast("技能移除成功")
                setResult(200)
                finish()
            }
        }

        vm.getAddSkillListData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                leftData.clear()
                leftAdapter.notifyDataSetChanged()
                rightData.clear()
                rightAdapter.notifyDataSetChanged()
            }
            onSuccess {
                dismissLoading()

                val list = mutableListOf<SkillAddBean>()
                // 过滤数据
                // 第一层过滤: 把三级为空的二级分类给去掉
                it?.data?.forEach {first ->
                    val secondList = first.small?.filter { !it.little_cate.isNullOrEmpty() }?.toMutableList()
                    if (secondList?.isNotEmpty() == true){
                        first.small = secondList
                        list.add(first)
                    }
                }
                if (list.isNotEmpty()){
                    list[0].is_checked = true
                    leftData.clear()
                    leftData.addAll(list.filter { it.small?.isNotEmpty() == true })
                    leftAdapter.notifyDataSetChanged()
                    rightData.clear()
                    rightData.addAll(list[0].small ?: listOf())
                    rightAdapter.notifyDataSetChanged()
                }else{
                    leftData.clear()
                    leftAdapter.notifyDataSetChanged()
                    rightData.clear()
                    rightAdapter.notifyDataSetChanged()
                }

            }
        }

    }

    override fun initImmersionBar() {
        immersionBar {
            statusBarDarkFont(false)
            transparentStatusBar()
            titleBarMarginTop(binding.titleBar)
        }
    }

}